from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains

import time

# 假设配置文件 'config.py' 包含用户名、密码和登录URL
from config import Selectors, USER_NAME, PASSWORD, LOGIN_URL, SALE_PASSWORD


def wait_for_element(driver, xpath, timeout=10):
    """
    等待直到页面元素出现
    """
    wait = WebDriverWait(driver, timeout)
    return wait.until(EC.presence_of_element_located((By.XPATH, xpath)))


def login(driver, phone, password):
    """
    执行登录操作
    """
    # 增加等待时间
    driver.implicitly_wait(30)

    # 确保页面完全加载
    WebDriverWait(driver, 30).until(
        EC.visibility_of_element_located((By.TAG_NAME, "body"))
    )

    # 定位手机号输入框
    phone_input = wait_for_element(driver, Selectors.PHONE_INPUT_XPATH)

    # 定位密码输入框
    password_input = wait_for_element(driver, Selectors.PASSWORD_INPUT_XPATH)

    # 阅读并同意
    image_button = wait_for_element(driver, Selectors.AGREE_IMAGE_XPATH)

    # 定位登录按钮
    login_button = wait_for_element(driver, Selectors.LOGIN_BUTTON_XPATH)

    phone_input.send_keys(phone)
    password_input.send_keys(password)
    image_button.click()

    time.sleep(3)
    login_button.click()


def getUserCenter(driver):
    # 增加等待时间
    driver.implicitly_wait(20)
    # 我的


# 交易
def get_market_list(driver):
    # 增加等待时间
    driver.implicitly_wait(30)

    # 交易
    market_button = wait_for_element(
        driver,
        Selectors.MARKET_BUTTON_XPATH,
    )
    market_button.click()


def get_my_list(driver):
    # 增加等待时间
    driver.implicitly_wait(20)

    # 交易
    market_button = wait_for_element(
        driver,
        Selectors.MARKET_BUTTON_XPATH,
    )
    market_button.click()


def navigate_to_post(driver):
    """
    导航到 '我要发布' 页面
    """
    driver.implicitly_wait(30)
    post_button = wait_for_element(driver, Selectors.POST_BUTTON_XPATH)
    post_button.click()


def navigate_to_my_posts(driver):
    """
    导航到 '我的发布' 页面
    """
    driver.implicitly_wait(30)
    my_posts_button = wait_for_element(driver, Selectors.MY_POSTS_BUTTON_XPATH)
    my_posts_button.click()


def navigate_to_my_orders(driver):
    """
    导航到 '我的订单' 页面
    """
    driver.implicitly_wait(20)
    my_orders_button = wait_for_element(driver, Selectors.MY_ORDERS_BUTTON_XPATH)
    my_orders_button.click()


def navigate_to_my_purchases(driver):
    """
    导航到 '我的买入' 页面
    """
    driver.implicitly_wait(20)
    my_purchases_button = wait_for_element(driver, Selectors.MY_PURCHASES_BUTTON_XPATH)
    my_purchases_button.click()


def navigate_to_my_sales(driver):
    """
    导航到 '我的出售' 页面
    """
    driver.implicitly_wait(20)
    my_sales_button = wait_for_element(driver, Selectors.MY_SALES_BUTTON_XPATH)
    my_sales_button.click()


def has_order_data(driver):
    driver.implicitly_wait(20)
    try:
        # 尝试查找包含"暂无数据"的元素
        no_data_element = driver.find_element(By.CLASS_NAME, "view_is_nodata")
        print("找到了元素 no_data_element")
        # 如果找到了元素，说明没有订单数据
        return False
    except:
        print("未找到了元素 no_data_element")
        # 如果未找到元素，说明有订单数据
        return True


def begin_sale_10(driver):
    driver.implicitly_wait(20)

    choose_number = wait_for_element(driver, Selectors.CHOOSE_NUMBER_XPATH)
    choose_number.click()

    input_number = wait_for_element(driver, Selectors.NUMBER_10_XPATH)
    time.sleep(2)  # 等待1秒，以确保页面稳定
    input_number.click()

    # 定位到包含文本“25”的元素
    # number_25_element = wait_for_element(driver, Selectors.NUMBER_25_XPATH)

    # driver.execute_script("arguments[0].scrollIntoView(true);", number_25_element)
    # time.sleep(2)  # 等待1秒，以确保页面稳定
    # # 点击该元素
    # number_25_element.click()

    time.sleep(2)  # 等待1秒，以确保页面稳定
    number_ok = wait_for_element(driver, Selectors.NUMBER_OK_XPATH)
    number_ok.click()

    password = wait_for_element(driver, Selectors.SALE_PASSWORD_INPUT_XPATH)
    password.send_keys(SALE_PASSWORD)

    sale_ok = wait_for_element(driver, Selectors.SALE_OK_XPATH)
    sale_ok.click()

    final_ok = wait_for_element(driver, Selectors.FINAL_OK_XPATH)
    final_ok.click()


def begin_sale_5(driver):
    driver.implicitly_wait(20)

    choose_number = wait_for_element(driver, Selectors.CHOOSE_NUMBER_XPATH)
    choose_number.click()

    input_number = wait_for_element(driver, Selectors.NUMBER_5_XPATH)
    time.sleep(2)  # 等待1秒，以确保页面稳定
    input_number.click()

    # 定位到包含文本“25”的元素
    # number_25_element = wait_for_element(driver, Selectors.NUMBER_25_XPATH)

    # driver.execute_script("arguments[0].scrollIntoView(true);", number_25_element)
    # time.sleep(2)  # 等待1秒，以确保页面稳定
    # # 点击该元素
    # number_25_element.click()

    time.sleep(2)  # 等待1秒，以确保页面稳定
    number_ok = wait_for_element(driver, Selectors.NUMBER_OK_XPATH)
    number_ok.click()

    password = wait_for_element(driver, Selectors.SALE_PASSWORD_INPUT_XPATH)
    password.send_keys(SALE_PASSWORD)

    sale_ok = wait_for_element(driver, Selectors.SALE_OK_XPATH)
    sale_ok.click()

    final_ok = wait_for_element(driver, Selectors.FINAL_OK_XPATH)
    final_ok.click()


# 撤销发布
def revoke_publication(driver):
    driver.implicitly_wait(20)
    revoke_publication = wait_for_element(driver, Selectors.Revoke_Publication)

    revoke_publication.click()
    final_ok = wait_for_element(driver, Selectors.FINAL_OK_XPATH)
    final_ok.click()


def main():
    # 设置浏览器驱动路径
    edge_driver_path = "/home/huangxin/webdriver/msedgedriver"

    # 创建 Edge 服务对象
    edge_service = EdgeService(executable_path=edge_driver_path)

    # 创建 Edge 浏览器对象，传递服务对象
    driver = webdriver.Edge(service=edge_service)

    # 打开登录页面
    driver.get(LOGIN_URL)

    # 执行登录操作
    login(driver, USER_NAME, PASSWORD)

    time.sleep(3)
    # 交易
    get_market_list(driver)

    time.sleep(3)
    # 我的订单
    navigate_to_my_orders(driver)
    time.sleep(3)
    while True:
        navigate_to_my_purchases(driver)
        time.sleep(5)
        if has_order_data(driver):
            print("我的买入有订单")
        else:
            print("我的买入无订单")

        navigate_to_my_sales(driver)
        time.sleep(5)
        if has_order_data(driver):
            print("我的出售有订单")
        else:
            print("我的出售无订单")

    time.sleep(30)
    # 其他功能实现...

    # 关闭浏览器
    # driver.quit()


if __name__ == "__main__":
    main()
